package com.bwie.assist.canal;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import com.rabbitmq.tools.json.JSONUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.Charset;

/**
 * @ClassName CanalRabbitMQListener
 * @Description TODO
 * @Author 李奉武
 * @Motto 逆水行舟, 不进则退
 * @Date 2022/4/4 17:04
 * @Version 1.0
 */
@Component
public class CanalRabbitMQListener {
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "canal.queue", durable = "true"),
                    exchange = @Exchange(value = "canal.exchange"),
                    key = "canal.routing.key"
            )
    })
    public void handleDataChange(Message mg,byte[] message, Channel channel) throws IOException {
        String  realMsg = new String(message, Charset.defaultCharset());
        System.out.println(JSON.toJSONString(realMsg));
        JsonRootBean canalMessage = JSON.parseObject(message, JsonRootBean.class);
        System.out.println(canalMessage.toString());
        String tableName = canalMessage.getTable();
        channel.basicAck(mg.getMessageProperties().getDeliveryTag(), false);

    }
}
